<Project Sdk="Microsoft.Build.NoTargets">

  <Sdk Name="Microsoft.DotNet.SharedFramework.Sdk" />

  <PropertyGroup>
    <TargetFramework>$(NetCoreAppCurrent)-$(TargetOS)</TargetFramework>
    <BuildInParallel>false</BuildInParallel>
    <IsPackable>false</IsPackable>
    <FrameworkListOutputPath>$(MicrosoftNetCoreAppRefPackDataDir)FrameworkList.xml</FrameworkListOutputPath>
    <!-- Import the illink file which contains some of the logic required to illink the shared framework assemblies. -->
    <ExplicitlyImportCustomILLinkTargets>true</ExplicitlyImportCustomILLinkTargets>
  </PropertyGroup>

  <ItemGroup>
    <!-- The sfx ref projects must be built first as sfx src projects don't use P2Ps to reference each other. Instead the
         references are defined manually via Reference items, therefore the reference assemblies must exist before that. -->
    <ProjectReference Include="sfx-ref.proj">
      <OutputItemType Condition="'$(RefOnly)' == 'true'">SharedFrameworkAssembly</OutputItemType>
    </ProjectReference>
     <!-- The RefOnly condition exists to support building the reference source projects only. -->
    <ProjectReference Include="sfx-src.proj" OutputItemType="SharedFrameworkAssembly" Condition="'$(RefOnly)' != 'true'" />
    <ProjectReference Include="sfx-gen.proj" OutputItemType="SharedFrameworkAnalyzerAssembly" />
  </ItemGroup>

  <Target Name="GetGenerateFrameworkListFileInputs">
    <Error Text="Shared framework assemblies must be built before the framework list can be generated."
           Condition="'@(SharedFrameworkAssembly)' == '' or '@(SharedFrameworkAnalyzerAssembly)' == ''" />

    <ItemGroup>
      <SharedFrameworkAssemblyWithReferenceAssembly Include="@(SharedFrameworkAssembly->WithMetadataValue('IsPrivateAssembly', 'false')->HasMetadata('ReferenceAssembly'))" />
      <SharedFrameworkAssemblyWithoutReferenceAssembly Include="@(SharedFrameworkAssembly->WithMetadataValue('IsPrivateAssembly', 'false'))"
                                                       Exclude="@(SharedFrameworkAssemblyWithReferenceAssembly)" />

      <FrameworkListInputFile Include="@(SharedFrameworkAssemblyWithReferenceAssembly->Metadata('ReferenceAssembly'));
                                       @(SharedFrameworkAssemblyWithoutReferenceAssembly)"
                              TargetPath="ref/$(NetCoreAppCurrent)" />
      <FrameworkListInputFile Include="@(SharedFrameworkAnalyzerAssembly)"
                              TargetPath="%(SharedFrameworkAnalyzerAssembly.GeneratorProjectBaseTargetPath)" />
    </ItemGroup>
  </Target>

  <!-- Generate the targeting pack's framework list so that out-of-band projects can leverage it. -->
  <UsingTask TaskName="CreateFrameworkListFile" AssemblyFile="$(DotNetSharedFrameworkTaskFile)"/>
  <Target Name="GenerateFrameworkListFile"
          DependsOnTargets="GetGenerateFrameworkListFileInputs"
          AfterTargets="Build"
          Inputs="@(FrameworkListInputFile)"
          Outputs="$(FrameworkListOutputPath)">
    <ItemGroup>
      <FrameworkListRootAttribute Include="Name" Value="$(NetCoreAppCurrentBrandName)" />
      <FrameworkListRootAttribute Include="TargetFrameworkIdentifier" Value="$(NetCoreAppCurrentIdentifier)" />
      <FrameworkListRootAttribute Include="TargetFrameworkVersion" Value="$(NetCoreAppCurrentVersion)" />
      <FrameworkListRootAttribute Include="FrameworkName" Value="$(MicrosoftNetCoreAppFrameworkName)" />
    </ItemGroup>

    <CreateFrameworkListFile Files="@(FrameworkListInputFile)"
                             TargetFile="$(FrameworkListOutputPath)"
                             TargetFilePrefixes="ref/;runtimes/;analyzers/"
                             RootAttributes="@(FrameworkListRootAttribute)" />
  </Target>

  <!-- Copy the intellisense file to the targeting pack directory. -->
  <Target Name="CopyDocumentationFiles"
          AfterTargets="Build"
          Condition="'$(RefOnly)' != 'true'">
    <ItemGroup>
      <DocumentationFileToCopy Include="@(SharedFrameworkAssembly->WithMetadataValue('IsPrivateAssembly', 'false')->Metadata('DocFileItem'))" />
    </ItemGroup>

    <Copy SourceFiles="@(DocumentationFileToCopy)"
          DestinationFolder="$(MicrosoftNetCoreAppRefPackRefDir)"
          SkipUnchangedFiles="true"
          UseHardlinksIfPossible="true" />
  </Target>

  <Target Name="GetTrimSharedFrameworkAssembliesInputs"
          DependsOnTargets="ResolveProjectReferences">
    <PropertyGroup>
      <SharedFrameworkAssembliesTrimDir>$([MSBuild]::NormalizeDirectory('$(IntermediateOutputPath)', 'illink-$(TargetArchitecture)'))</SharedFrameworkAssembliesTrimDir>
      <SharedFrameworkAssembliesTrimMarkerFile>$(SharedFrameworkAssembliesTrimDir)linker-marker.txt</SharedFrameworkAssembliesTrimMarkerFile>
    </PropertyGroup>

    <ItemGroup>
      <SharedFrameworkSuppressionsXml Include="@(SharedFrameworkAssembly->Metadata('ILLinkSuppressionsXmls'))" />
      <SharedFrameworkSuppressionsXml Include="$(CoreLibSharedDir)ILLink\ILLink.Suppressions.LibraryBuild.xml" />
    </ItemGroup>
  </Target>

  <Target Name="TrimSharedFrameworkAssemblies"
          AfterTargets="Build"
          DependsOnTargets="ResolveProjectReferences;GetTrimSharedFrameworkAssembliesInputs;PrepareForAssembliesTrim"
          Condition="'$(RefOnly)' != 'true'"
          Inputs="@(SharedFrameworkAssembly);@(SharedFrameworkSuppressionsXml);$(ILLinkTasksAssembly)"
          Outputs="$(SharedFrameworkAssembliesTrimMarkerFile)">
    <Message Text="$(MSBuildProjectName) -> Trimming $(OutputRID) shared framework assemblies with ILLinker..." Importance="high" />

    <PropertyGroup>
      <SharedFrameworkILLinkArgs>$(ILLinkArgs)</SharedFrameworkILLinkArgs>
      <!-- update debug symbols -->
      <SharedFrameworkILLinkArgs>$(SharedFrameworkILLinkArgs) -b true</SharedFrameworkILLinkArgs>
      <SharedFrameworkILLinkArgs Condition="'@(SharedFrameworkSuppressionsXml)' != ''" >$(SharedFrameworkILLinkArgs) --link-attributes "@(SharedFrameworkSuppressionsXml, '" --link-attributes "')"</SharedFrameworkILLinkArgs>
    </PropertyGroup>

    <ItemGroup>
      <SharedFrameworkAssembly RootMode="library" />
    </ItemGroup>

    <MakeDir Directories="$(SharedFrameworkAssembliesTrimDir)" />
    <ILLink AssemblyPaths=""
        RootAssemblyNames="@(SharedFrameworkAssembly)"
        OutputDirectory="$(SharedFrameworkAssembliesTrimDir)"
        ExtraArgs="$(SharedFrameworkILLinkArgs)"
        ToolExe="$(_DotNetHostFileName)"
        ToolPath="$(_DotNetHostDirectory)" />

    <!-- Create a marker file which serves as the target's output to enable incremental builds. -->  
    <Touch Files="$(SharedFrameworkAssembliesTrimMarkerFile)"
           AlwaysCreate="true" />
  </Target>

</Project>
